package com.gxa.aiface.service;

import com.gxa.aiface.util.BaiduAiUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class AIFaceService {

    @Resource
    private BaiduAiUtils baiduAiUtils;

    //识别人脸
    public Map<String, Object> checkface(StringBuffer imagebast64) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 60002);
        map.put("msg", "验证人脸失败");

        String image = imagebast64.substring(imagebast64.indexOf(",") + 1, imagebast64.length());
        String userId = baiduAiUtils.faceSearch(image);
        System.out.println("checkface userid=" + userId);
        if(userId != null) {
            map.put("code", 200);
            map.put("msg", "验证人脸成功");
            map.put("userId", userId);
        }
        return map;
    }

    //注册人脸
    public Map<String, Object> registerFace(StringBuffer imagebast64){
        Map<String, Object> map = new HashMap<>();
        map.put("code", 60001);
        map.put("msg", "注册失败");

        // 注册用户编号  这里模拟用户登录行为  业务场景是当用户第一次通过手动输入密码并且成功登录以后，可以调用人脸识别采集信息，后面的登录可以使用人脸识别完成
        String userId = "8888";
        String image = imagebast64.substring(imagebast64.indexOf(",") + 1, imagebast64.length());   // 拆分图片数据，固定格式
        Boolean isSuccess = baiduAiUtils.faceRegister(userId, image);                               // 调用百度官方utils工具包完成用户数据注册
        // 上面代码在执行之后，后台服务会输出注册结果如下：
        // {"result":{"face_token":"124da5c979b1faf9bb777e34c48b80fc","location":{"top":222.67,"left":181.69,"rotation":-1,"width":196,"height":190}},"log_id":1795978355,"error_msg":"SUCCESS","cached":0,"error_code":0,"timestamp":1721520178}
        System.out.println("注册状态 isSuccess=" + isSuccess);
        if (isSuccess){
            map.put("code", 200);
            map.put("userId", userId);
            map.put("msg", "注册成功");
        }
        return map;
    }
}
